﻿using DLL.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLL.Repository
{
    public class EmployeeRepository : IEmployeeRepository
    {
        private readonly AppDbContext _db;

        public EmployeeRepository(AppDbContext db)
        {
            _db = db;
        }
        public async Task Create(Employee item)
        {
            var entry = _db.Entry(item);
            if (entry.State == EntityState.Detached)
                _db.Employees.Add(item);
            await _db.SaveChangesAsync();
        }

        public async Task Delete(Employee item)
        {
            var result = await GetById(item.Id);
            if (result != null)
            {
                _db.Employees.Remove(item);
                await _db.SaveChangesAsync();
            }
        }

        public async Task<IEnumerable<Employee>> GetAll()
        {
            return await _db.Employees
                //.Include(employee => employee.workShifts)
                //.Include(e => e.JobPosition)
                .ToListAsync();
        }

        public async Task<Employee> GetById(Guid id)
        {
           return await _db.Employees
                .Include(employee => employee.workShifts)
                //.Include(e=> e.JobPosition)
                .FirstOrDefaultAsync(i => i.Id == id);            
        }

        public async Task Update(Employee item)
        {
            var result = await GetById(item.Id);
            if (result != null)
            {
                _db.Employees.Update(item);
                await _db.SaveChangesAsync();
            }
        }
    }
}
